package io.dbw.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Locale;

/**
 * 国际化解析
 */
public class MessageResolverConfig implements LocaleResolver {

    private static final String LANG = "lang";

    private static final String LANG_SESSION = "lang_session";

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        Locale locale;
        String language = request.getHeader(LANG);
        //中文language=zh_CN
        if (StringUtils.isNotEmpty(language)) {
            locale = new Locale(language.toLowerCase());
        } else {
            locale = new Locale("zh_cn");
        }
        HttpSession session = request.getSession();
        session.setAttribute(LANG_SESSION, locale);
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }

}

